home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 4 / Example 4.1 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  6.7 KB  |  259 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 4.1: Heightmaps from Images                        //
  3. // Written by: C. Granberg, 2005                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include "debug.h"
  9. #include "heightMap.h"
  10.  
  11. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  12. #define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  13.  
  14. class APPLICATION
  15. {
  16.     public:
  17.         APPLICATION();
  18.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  19.         HRESULT Update(float deltaTime);
  20.         HRESULT Render();
  21.         HRESULT Cleanup();
  22.         HRESULT Quit();
  23.  
  24.     private:
  25.         IDirect3DDevice9* m_pDevice; 
  26.         HEIGHTMAP *m_pHeightMap;
  27.  
  28.         int m_image;
  29.         float m_angle;
  30.         HWND m_mainWindow;
  31.         ID3DXFont *m_pFont;
  32. };
  33.  
  34. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  35. {
  36.     APPLICATION app;
  37.  
  38.     if(FAILED(app.Init(hInstance, 800, 600, true)))
  39.         return 0;
  40.  
  41.     MSG msg;
  42.     memset(&msg, 0, sizeof(MSG));
  43.     int startTime = timeGetTime(); 
  44.  
  45.     while(msg.message != WM_QUIT)
  46.     {
  47.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  48.         {
  49.             ::TranslateMessage(&msg);
  50.             ::DispatchMessage(&msg);
  51.         }
  52.         else
  53.         {    
  54.             int t = timeGetTime();
  55.             float deltaTime = (t - startTime)*0.001f;
  56.  
  57.             app.Update(deltaTime);
  58.             app.Render();
  59.  
  60.             startTime = t;
  61.         }
  62.     }
  63.  
  64.     app.Cleanup();
  65.  
  66.     return msg.wParam;
  67. }
  68.  
  69. APPLICATION::APPLICATION()
  70. {
  71.     m_pDevice = NULL; 
  72.     m_pHeightMap = NULL;
  73.     m_mainWindow = 0;
  74.     m_angle = 0.0f;
  75.     m_image = 0;
  76. }
  77.  
  78. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  79. {
  80.     debug.Print("Application initiated");
  81.  
  82.     //Create Window Class
  83.     WNDCLASS wc;
  84.     memset(&wc, 0, sizeof(WNDCLASS));
  85.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  86.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  87.     wc.hInstance     = hInstance;
  88.     wc.lpszClassName = "D3DWND";
  89.  
  90.     //Register Class and Create new Window
  91.     RegisterClass(&wc);
  92.     m_mainWindow = CreateWindow("D3DWND", "Example 4.1: Heightmaps from Images", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  93.     SetCursor(NULL);
  94.     ShowWindow(m_mainWindow, SW_SHOW);
  95.     UpdateWindow(m_mainWindow);
  96.  
  97.     //Create IDirect3D9 Interface
  98.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  99.  
  100.     if(d3d9 == NULL)
  101.     {
  102.         debug.Print("Direct3DCreate9() - FAILED");
  103.         return E_FAIL;
  104.     }
  105.  
  106.     //Check that the Device supports what we need from it
  107.     D3DCAPS9 caps;
  108.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  109.  
  110.     //Hardware Vertex Processing or not?
  111.     int vp = 0;
  112.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  113.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  114.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  115.  
  116.     //Check vertex & pixelshader versions
  117.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  118.     {
  119.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  120.     }
  121.  
  122.     //Set D3DPRESENT_PARAMETERS
  123.     D3DPRESENT_PARAMETERS d3dpp;
  124.     d3dpp.BackBufferWidth            = width;
  125.     d3dpp.BackBufferHeight           = height;
  126.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  127.     d3dpp.BackBufferCount            = 1;
  128.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  129.     d3dpp.MultiSampleQuality         = 0;
  130.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  131.     d3dpp.hDeviceWindow              = m_mainWindow;
  132.     d3dpp.Windowed                   = windowed;
  133.     d3dpp.EnableAutoDepthStencil     = true; 
  134.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  135.     d3dpp.Flags                      = 0;
  136.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  137.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  138.  
  139.     //Create the IDirect3DDevice9
  140.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  141.                                  vp, &d3dpp, &m_pDevice)))
  142.     {
  143.         debug.Print("Failed to create IDirect3DDevice9");
  144.         return E_FAIL;
  145.     }
  146.  
  147.     //Release IDirect3D9 interface
  148.     d3d9->Release();
  149.  
  150.     D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
  151.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  152.                    DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);
  153.  
  154.     return S_OK;
  155. }
  156.  
  157. HRESULT APPLICATION::Update(float deltaTime)
  158. {    
  159.     if(m_pHeightMap == NULL)        //Create Heightmap
  160.     {
  161.         //Load heightmap from file
  162.         m_pHeightMap = new HEIGHTMAP(m_pDevice, INTPOINT(100, 100));
  163.         if(FAILED(m_pHeightMap->LoadFromFile("images/abe.jpg")))
  164.         {
  165.             debug.Print("Failed to load from file");
  166.             Quit();
  167.         }
  168.  
  169.         //Create particles to visualize the heightmap
  170.         if(FAILED(m_pHeightMap->CreateParticles()))
  171.         {
  172.             debug.Print("Failed to create particles");
  173.             Quit();
  174.         }
  175.     }
  176.     else
  177.     {
  178.         //Control camera
  179.         m_angle += deltaTime * 0.5f;
  180.         D3DXMATRIX  matWorld, matView, matProj;        
  181.         D3DXVECTOR2 centre = m_pHeightMap->GetCentre();
  182.         D3DXVECTOR3 Eye    = D3DXVECTOR3(centre.x + cos(m_angle) * centre.x * 2.0f, m_pHeightMap->m_maxHeight * 8.0f, -centre.y + sin(m_angle) * centre.y * 2.0f);
  183.         D3DXVECTOR3 Lookat = D3DXVECTOR3(centre.x, 0.0f,  -centre.y);
  184.  
  185.         D3DXMatrixIdentity(&matWorld);
  186.         D3DXMatrixLookAtLH(&matView, &Eye, &Lookat, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  187.         D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.3333f, 1.0f, 1000.0f );
  188.  
  189.         m_pDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  190.         m_pDevice->SetTransform( D3DTS_VIEW,       &matView );
  191.         m_pDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  192.  
  193.         //Change heightmap image
  194.         if(KEYDOWN(VK_SPACE))
  195.         {
  196.             m_image++;
  197.             if(m_image > 2)m_image = 0;
  198.             if(m_image == 0)m_pHeightMap->LoadFromFile("images/abe.jpg");
  199.             if(m_image == 1)m_pHeightMap->LoadFromFile("images/smiley.bmp");
  200.             if(m_image == 2)m_pHeightMap->LoadFromFile("images/heightmap.jpg");
  201.  
  202.             m_pHeightMap->CreateParticles();
  203.             Sleep(300);
  204.         }
  205.     }
  206.  
  207.     if(KEYDOWN(VK_ESCAPE))
  208.         Quit();
  209.  
  210.     return S_OK;
  211. }    
  212.  
  213. HRESULT APPLICATION::Render()
  214. {
  215.     // Clear the viewport
  216.     m_pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L );
  217.  
  218.     // Begin the scene 
  219.     if(SUCCEEDED(m_pDevice->BeginScene()))
  220.     {
  221.         if(m_pHeightMap != NULL)m_pHeightMap->Render();
  222.  
  223.         RECT r = {630, 10, 800, 30};
  224.         m_pFont->DrawText(NULL, "Space: Change Image", -1, &r, DT_LEFT | DT_NOCLIP | DT_TOP, 0xffffffff);
  225.  
  226.         // End the scene.
  227.         m_pDevice->EndScene();
  228.         m_pDevice->Present(0, 0, 0, 0);
  229.     }
  230.  
  231.     return S_OK;
  232. }
  233.  
  234. HRESULT APPLICATION::Cleanup()
  235. {
  236.     try
  237.     {
  238.         if(m_pHeightMap != NULL)
  239.         {
  240.             delete m_pHeightMap;
  241.             m_pHeightMap = NULL;
  242.         }
  243.  
  244.         m_pFont->Release();
  245.         m_pDevice->Release();
  246.  
  247.         debug.Print("Application terminated");
  248.     }
  249.     catch(...){}
  250.  
  251.     return S_OK;
  252. }
  253.  
  254. HRESULT APPLICATION::Quit()
  255. {    
  256.     ::DestroyWindow(m_mainWindow);
  257.     ::PostQuitMessage(0);
  258.     return S_OK;
  259. }